/**
 * Created by liuy on 2016/9/26.
 */

var logsStatistics = function () {

    //校验字段
    var checkValue = function () {
        var validateMessageBox = $('#validateMessageBox');
        var configEditForm = $('#configEditForm');
        configEditForm.validate({
            errorElement: 'label',
            errorClass: 'help-block',
            focusInvalid: true,
            rules: {
                livelyTime: {
                    required: true,
                    max: 720,
                    min: 24,
                    digits: true
                },
                recordNumber: {
                    required: true,
                    max: 1000000,
                    min: 1,
                    digits: true
                },
                recordAlarm: {
                    required: true,
                    min: 1,
                    digits: true
                }
            },
            messages: {
                livelyTime: {
                    required: "平台设备活跃值为必填字段!",
                    max: "平台设备活跃值最大不能超过720小时！",
                    min: "平台设备活跃值最小不能低于24小时！",
                    digits: "平台设备活跃值必须是纯数字！"
                },
                recordNumber: {
                    required: "审计迹最大值为必填字段!",
                    max: "审计迹最大值不能超过1百万！",
                    min: "审计迹最大值不能低于1！",
                    digits: "审计迹最大值必须是纯数字！"
                },
                recordAlarm: {
                    required: "审计迹提醒值为必填字段!",
                    min: "审计迹提醒值不能低于1！",
                    digits: "审计迹提醒值必须是纯数字！"
                },
            },
            highlight: function (element) {
                $(element).closest('.check-class').removeClass('has-success').addClass('has-error');
            },
            unhighlight: function (element) {
                $(element).closest('.check-class').removeClass('has-error').addClass('has-success');
            },
            success: function (label, element) {
                label.closest('.check-class').removeClass('has-error');
            },
            submitHandler: function (form) {
                App.blockUI(window, true);
                form.submit();
            },
            errorContainer: validateMessageBox,
            errorPlacement: function (error, element) {
                this.errorContainer.addClass('note-danger').html("<p>输入有误，请先更正。</p>").show();
                var formGroup = $(element).closest('.check-class');
                var errorElement = formGroup.children('span.' + this.errorClass);
                if (!errorElement || errorElement.length == 0) {
                    errorElement = $('<span/>').addClass(this.errorClass);
                    errorElement.appendTo(formGroup);
                }
                error.appendTo(errorElement);
            }
        });
    }


    //记录能够修改参数值
    var changeInputTrue = function () {
        $("#make").on("click", function () {
            $("[name=recordAlarm]").attr("readonly", false);
            $("[name=recordNumber]").attr("readonly", false);
        })
    };

    //不记录不能够修改参数值
    var changeInputFalse = function () {
        $("#noMake").on("click", function () {
            $("[name=recordAlarm]").attr("readonly", true);
            $("[name=recordNumber]").attr("readonly", true);
        })
    };

    return {
        //初始化
        init: function () {
            checkValue();
            changeInputTrue();
            changeInputFalse();

            $('#activeCount').bind('input propertychange', function () {
                this.value = this.value.replace(/\D/g, '');
            });

            $('#maxCount').bind('input propertychange', function () {
                this.value = this.value.replace(/\D/g, '');
            });

            $('#errorCount').bind('input propertychange', function () {
                this.value = this.value.replace(/\D/g, '');
            });
        }
    }

}();
